package com.example.dictionary.repository;

import com.example.memento.model.WordEntry;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

import static com.example.memento.model.table.WordEntryTableDef.WORD_ENTRY;

/**
 * 词典词条Mapper
 * 使用MyBatis-Flex进行数据访问
 */
@Mapper
public interface WordEntryMapper extends BaseMapper<WordEntry> {
    
    /**
     * 根据单词搜索
     */
    @Select("SELECT * FROM word_entries WHERE word = #{word}")
    List<WordEntry> findByWord(@Param("word") String word);
    
    /**
     * 模糊搜索单词
     */
    default List<WordEntry> findByWordLike(String keyword) {
        return selectListByQuery(QueryWrapper.create()
                .where(WORD_ENTRY.WORD.like(keyword))
                .orderBy(WORD_ENTRY.FREQUENCY.desc()));
    }
    
    /**
     * 根据词典源搜索
     */
    default List<WordEntry> findBySource(String source) {
        return selectListByQuery(QueryWrapper.create()
                .where(WORD_ENTRY.SOURCE.eq(source))
                .orderBy(WORD_ENTRY.FREQUENCY.desc()));
    }
    
    /**
     * 根据语言搜索
     */
    default List<WordEntry> findByLanguage(String language) {
        return selectListByQuery(QueryWrapper.create()
                .where(WORD_ENTRY.LANGUAGE.eq(language))
                .orderBy(WORD_ENTRY.FREQUENCY.desc()));
    }
    
    /**
     * 根据词性搜索
     */
    default List<WordEntry> findByPartOfSpeech(String partOfSpeech) {
        return selectListByQuery(QueryWrapper.create()
                .where(WORD_ENTRY.PART_OF_SPEECH.eq(partOfSpeech))
                .orderBy(WORD_ENTRY.FREQUENCY.desc()));
    }
    
    /**
     * 复合搜索
     */
    default List<WordEntry> searchByMultipleConditions(String word, String source, String language) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        
        if (word != null && !word.trim().isEmpty()) {
            queryWrapper.and(WORD_ENTRY.WORD.like(word));
        }
        if (source != null && !source.trim().isEmpty()) {
            queryWrapper.and(WORD_ENTRY.SOURCE.eq(source));
        }
        if (language != null && !language.trim().isEmpty()) {
            queryWrapper.and(WORD_ENTRY.LANGUAGE.eq(language));
        }
        
        return selectListByQuery(queryWrapper.orderBy(WORD_ENTRY.FREQUENCY.desc()));
    }
    
    /**
     * 获取高频词汇
     */
    default List<WordEntry> findHighFrequencyWords(int limit) {
        return selectListByQuery(QueryWrapper.create()
                .where(WORD_ENTRY.FREQUENCY.isNotNull())
                .orderBy(WORD_ENTRY.FREQUENCY.desc())
                .limit(limit));
    }
    
    /**
     * 统计词条数量
     */
    @Select("SELECT COUNT(*) FROM word_entries")
    long countAll();
    
    /**
     * 按词典源统计
     */
    @Select("SELECT source, COUNT(*) as count FROM word_entries GROUP BY source")
    List<Object> countBySource();
    
    /**
     * 按语言统计
     */
    @Select("SELECT language, COUNT(*) as count FROM word_entries WHERE language IS NOT NULL GROUP BY language")
    List<Object> countByLanguage();
}
